//
//  News.swift
//  Jock
//
//  Created by HD on 15/3/12.
//  Copyright (c) 2015年 Haidy. All rights reserved.
//

import UIKit

class News {
    
    var image: String!
    var published_at = 0
    var tag: String = ""
    var user: User!
    var image_size: ImageSize!
    var id: String = ""
    var votesDown = 0           //踩
    var votesUp = 0             //赞
    var created_at = 0
    var content = ""
    var state = ""
    var comments_count = 0
    var allow_comment = true

    class func analyse(json: JSON) -> [News] {
        var news: [News] = [News]()
        let array = json["items"]
        for (index: String, subJson: JSON) in array {
            var newNews = News()
            newNews.image = subJson["image"].string
            newNews.published_at = subJson["published_at"].intValue
            newNews.tag = subJson["tag"].string!
            let user = subJson["user"]
            if subJson["user"].type != Type.Null {
                newNews.user = User.analyse(user)
            }
            let imageSize = subJson["image_size"]
            if imageSize.type != Type.Null {
                newNews.image_size = ImageSize.analyse(imageSize)
            }
            newNews.id = subJson["id"].string!
            newNews.votesDown = subJson["votes"]["down"].intValue
            newNews.votesUp = subJson["votes"]["up"].intValue
            newNews.created_at = subJson["created_at"].intValue
            newNews.content = subJson["content"].string!
            newNews.state = subJson["state"].string!
            newNews.comments_count = subJson["comments_count"].intValue
            newNews.allow_comment = subJson["allow_comment"].boolValue
            news.append(newNews)
        }
        return news
    }
    
}